package com.fzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.common.tool.Medthods;
import com.fzg.entity.Song;
import com.fzg.entity.SongList;
import com.fzg.mapper.SongListMapper;
import com.fzg.mapper.SongMapper;
import com.fzg.service.SongListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzg
 * @since 2022-07-28
 */
@Service
public class SongListServiceImpl extends ServiceImpl<SongListMapper, SongList> implements SongListService {

    @Autowired
    private SongListMapper songListMapper;

    @Autowired
    private SongMapper songMapper;

    /**
     * 获取推荐的歌单列表
     *
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper getRecommendSongList(String searchKey) {

        List<SongList> songLists = searchKey.equals("") ? songListMapper.selectList(new LambdaQueryWrapper<SongList>().orderByDesc(SongList::getClickNum)) : songListMapper.selectList(new LambdaQueryWrapper<SongList>().like(SongList::getName,searchKey).orderByDesc(SongList::getClickNum));

        ArrayList<Object> result = new ArrayList<>();
        for (SongList songList : songLists) {
            HashMap<String, Object> item = new HashMap<>();
            item.put("songListAid",songList.getAid());
            item.put("songListName",songList.getName());
            item.put("songListPicUrl",songList.getPic());
            item.put("clickNum", Medthods.integerToContainChineseFontString(songList.getClickNum()));
            result.add(item);
        }
        return ResponseWrapper.markSuccess(result);
    }

    /**
     * 获取这个歌单的所有信息
     *
     * @param searchKey 搜索关键字
     * @param songListAid 歌单id
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper getThisSongListAllInfo(String searchKey, Integer songListAid) {
        HashMap<String, Object> result = new HashMap<>();
        // 根据歌单aid查询歌信息
        SongList songList = songListMapper.selectById(songListAid);
        // 歌单名字
        result.put("songListName",songList.getName());
        // 歌单的歌曲有多少首
        List<Song>  songs  = songMapper.selectList(new LambdaQueryWrapper<Song>()
                .eq(Song::getSongListAid, songListAid)
                .like(!searchKey.equals(""),Song::getName,searchKey)
        );

        result.put("songListLength",songs.size());

        ArrayList<Object> songs1 = new ArrayList<>();
        // 歌单的歌曲列表
        for (Song song : songs) {
            HashMap<String, Object> item = new HashMap<>();
            item.put("songAid",song.getAid());
            item.put("songName",song.getName());
            item.put("tags",song.getTags()==null ? "" : song.getTags());
            item.put("singer",song.getSinger());
            songs1.add(item);
        }
        result.put("songs",songs1);
        return ResponseWrapper.markSuccess(result);
    }
}
